package com.dlb.andorid.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.App;
import com.dlb.andorid.ProductAdapter;
import com.dlb.andorid.R;
import com.dlb.andorid.XContract;
import com.dlb.andorid.bean.ProductBean;
import com.dlb.andorid.bean.ServerResult;
import com.dlb.andorid.presenter.JrbkPresenter;
import com.kwan.xframe.mvp.presenter.BasePresenter;
import com.kwan.xframe.mvp.view.activity.BaseCommonActivity;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class JrbkActivity extends BaseCommonActivity implements XContract.IJrbxView {


	JrbkPresenter mPresenter;
	List<ProductBean> mProducts = new ArrayList<>();

	@Override
	protected BasePresenter getBasePresenter() {
		mPresenter = new JrbkPresenter(this);
		return mPresenter;
	}
	BaseQuickAdapter<ProductBean, BaseViewHolder> mHomeAdapter;
	RecyclerView rv_home;
	@Override
	protected void initViews() {
		super.initViews();

		rv_home = findViewById(R.id.recycleView);
		mHomeAdapter = new ProductAdapter(mProducts);

		rv_home.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
		rv_home.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		rv_home.setLayerType(View.LAYER_TYPE_HARDWARE, null);
		rv_home.setHasFixedSize(true);

		//解决更新 闪烁
		rv_home.setItemAnimator(null);
		rv_home.setNestedScrollingEnabled(false);
		rv_home.setAdapter(mHomeAdapter);

		mHomeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				Bundle bundle = new Bundle();
				bundle.putLong("id", mProducts.get(position).getId());
				go2Activity(ProductDetailActivity.class, bundle, false);
			}
		});

		mHomeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
			@Override
			public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
				//toastMsg(""+mProducts.get(position).getId());
				Log.e("kwan", "car:" + mProducts.get(position).getId());
				if (view.getId() == R.id.iv_car) {
					Log.e("kwan", "car:" + mProducts.get(position).getId());
					showProgress("");
					mPresenter.addCar(mProducts.get(position).getId(), App.mUserInfo.getUid(), 1, mProducts.get(position).getType_id(), mProducts.get(position).getSale_price());
				}

			}
		});
	}

	@Override
	protected void initData() {
		showProgress("");
		mPresenter.get(1);
	}

	@Override
	protected int getTopViewId() {
		return 0;
	}

	@Override
	protected int getMainViewId() {
		return R.layout.activity_jrbk;
	}

	@Override
	protected int getBottomViewId() {
		return 0;
	}

	@Override
	protected String getTitleTxt() {
		return "今日爆款";
	}

	@Override
	public String getPageName() {
		return null;
	}

	@Override
	public void getJrbkSuccess(List<ProductBean> data) {
		dismissProgress();
		mProducts.clear();
		mProducts.addAll(data);
		mHomeAdapter.notifyDataSetChanged();
	}

	@Override
	public void addCarSuccess(ServerResult data) {
		dismissProgress();
		toastMsg(data.getMsg());
	}
}
